package com.icss.controller;

import com.icss.dao.IRecordDao;
import com.icss.domian.PageBean;
import com.icss.domian.QueryVO;
import com.icss.service.ItemService;
import com.icss.service.RecordService;
import com.icss.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.text.ParseException;

import java.util.List;

@Controller
public class RecordController {
    @Autowired
    private RecordService recordService;

    @Autowired
    private UserService userService;

    @Autowired
    private ItemService itemService;

    @Autowired
    private IRecordDao iRecordDao;
    //获取所有违法记录
    @RequestMapping("findAllRecord")
    public String toIndex(ModelMap map) {

        List<QueryVO> queryVO = recordService.findQueryVO();
        map.addAttribute("queryVO", queryVO);
        map.addAttribute("currentPage",0);
        return "showRecord";
    }
    //分页显示
    @RequestMapping("findAllRecordPage")
    public  String  findAllPage(Integer currentPage,ModelMap map){
//        当前第一页
        if (currentPage==null || currentPage <=0){
             currentPage = 1;
        }
        //默认每页显示3条
        int count = 3;

        PageBean pageBean = null;
        try {
            pageBean = recordService.selectPageBean(currentPage,count);
            map.addAttribute("queryVO",pageBean.getQueryVOList());
            map.addAttribute("currentPage",pageBean.getCurrentPage());
            System.out.println(pageBean.getTotalPage());
            map.addAttribute("totalPage",pageBean.getTotalPage());
            map.addAttribute("count",count);
            map.addAttribute("countSize",pageBean.getTotalCount());

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "showRecord";
    }
//按车牌号模糊查询，完成按违法开始时间和违法结束时间执行时间区间查询功能
    @RequestMapping("selectRecord")
    public String SelectByTimeAndCord(String RecordLicense, String beginTime, String endTime, HttpSession session,ModelMap map){
        System.out.println("RecordLicense"+RecordLicense+"--------beginTime"+beginTime+"---------endtime"+endTime);

        List<QueryVO> licenseList = recordService.findByTimeAndCordLicense(beginTime, endTime, RecordLicense);
        map.addAttribute("queryVO",licenseList);
        map.addAttribute("currentPage",0);
        return "showRecord";
    }
    @Transactional
    @RequestMapping("UpdateIllegalItem")
    public  String  UpdateIllegalItem(String illegalRecordTime, Integer userId, String illegalRecordLicense,
                                      String phone, Integer illegalItemId, String illegalItemCode ,
                                      String illegalItemName, String illegalItemFine, Integer illegalItemScore
            , String illegalRecordAddress, HttpServletRequest request) throws ParseException {

        System.out.println(illegalRecordTime);
        List<QueryVO> licenseByUserId = userService.findRecordLicenseByUserId(userId);
        String RecordLicense = licenseByUserId.get(0).getIllegalRecordLicense();
        String phoneNumber = licenseByUserId.get(0).getPhone();
        if (phone.equalsIgnoreCase(phoneNumber) && illegalRecordLicense.equalsIgnoreCase(RecordLicense)){
//      如果电话号码和车牌号没改变的话 就直接添加违法记录
            recordService.updateIllegalItem(userId,illegalItemId,illegalRecordTime,
                    illegalRecordAddress,illegalRecordLicense);
//     userID          availableScore= availableScore -illegalItemScore可用分数
//                     unavailableScore+illegalItemScore 以记分数
            userService.updateIllegalCountAndSocre(userId,illegalItemScore);
            itemService.updateIllegalItemCount(illegalItemCode);
        }else {
//          更新手机号和车牌号
            recordService.updateRecordLicenseByUserId(userId,RecordLicense);
            userService.updateUserPhoneByUserID(userId,phone);
//            执行更新
            recordService.updateIllegalItem(userId,illegalItemId,illegalRecordTime,
                    illegalRecordAddress,illegalRecordLicense);
            System.out.println("修改手机号和车牌号");
        }
        return "redirect:findAllRecord";

    }

    @RequestMapping(value = "updateIllegalRecordStatus",method = RequestMethod.GET)
    public  String  updateIllegalRecordStatus(Integer illegalRecordId,HttpServletRequest request){
        recordService.updateIllegalRecordStatusById(illegalRecordId);
        userService.updateUnsettledIllegalCount(illegalRecordId);
        return "redirect:/findAllRecord";

    }



}
